// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SkymateVersion.cs" company="Skymate">
//   Copyright © 2015 Skymate. All rights reserved.
// </copyright>
// <summary>
//   skymate 类库版本.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Skymate
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    using Skymate.Extensions;

    /// <summary>
    ///     skymate 版本.
    /// </summary>
    public static class SkymateVersion
    {
        #region Static Fields

        /// <summary>
        /// The info version.
        /// </summary>
        private static readonly Version InfoVersion = new Version("1.0.0.0");

        /// <summary>
        /// The breaking changes history.
        /// </summary>
        private static readonly List<Version> BreakingChangesHistories = new List<Version>
                                                                             {
                                                                                 // IMPORTANT: Add skymate versions from low to high
                                                                                 // NOTE: do not specify build & revision unless you have good reasons for it.
                                                                                 //       A release with breaking changes should definitely have at least
                                                                                 //       a greater minor version.
                                                                                 new Version("1.0"),
                                                                                 new Version("1.0.1"),

                                                                                 // MC: had to be :-(
                                                                                 new Version("1.0"),
                                                                                 new Version("1.0")
                                                                             };

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes static members of the <see cref="SkymateVersion"/> class.
        /// </summary>
        static SkymateVersion()
        {
            BreakingChangesHistories.Reverse();

            // get informational version
            var infoVersionAttr =
                Assembly.GetExecutingAssembly().GetAttribute<AssemblyInformationalVersionAttribute>(false);
            if (infoVersionAttr != null)
            {
                InfoVersion = new Version(infoVersionAttr.InformationalVersion);
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the app version
        /// </summary>
        public static string CurrentVersion
        {
            get
            {
                return "{0}.{1}".FormatInvariant(InfoVersion.Major, InfoVersion.Minor);
            }
        }

        /// <summary>
        /// Gets the app full version
        /// </summary>
        public static string CurrentFullVersion
        {
            get
            {
                return InfoVersion.ToString();
            }
        }

        /// <summary>
        /// Gets the version.
        /// </summary>
        public static Version Version
        {
            get
            {
                return InfoVersion;
            }
        }

        /// <summary>
        /// Gets a list of SmartStore.NET versions in which breaking changes occured,
        /// which could lead to plugins malfunctioning.
        /// </summary>
        /// <remarks>
        /// A plugin's <c>MinAppVersion</c> is checked against this list to assume
        /// it's compatibility with the current app version.
        /// </remarks>
        internal static IEnumerable<Version> BreakingChangesHistory
        {
            get
            {
                return BreakingChangesHistories.AsEnumerable();
            }
        }

        #endregion
    }
}